<?php

Raise::load('core.RaiseObject');
Raise::load('core.helper.convert.unit.RaiseBaseConverter');

/**
 * RaiseUIControl class
 * A control that exists on the View level
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @abstract
 * @package phpraise.core.ui
 * @since 1.2
 */
abstract class RaiseUIControl extends RaiseObject {

    /**
     * The ID to use on the client side
     * @var string
     */
    protected $clientId;

    /**
     * Data Tag - any information to be tied with the control
     * @var mixed
     */
    protected $dataTag;

    /**
     * The page that the current control is tied to
     * @var RaisePage
     */
    protected $page;

    /**
     * Create a new RaiseUIControl
     * @ignore
     */
    function __construct(){

    }

    /**
     * Return the HTML output for this control
     * @return string The final output of this control
     * @abstract
     */
    public abstract function output();

    /**
     * Get or set the unique ID to be used on the client side.
     * @param string $id (Optional) The new client ID to set
     * @return string
     */
    public function clientId($id = false){
        if(func_num_args() == 1){
            $this->clientId = $id;
        }
        return $this->clientId;
    }

    /**
     * Add a piece of data that is tied to this control
     * @param mixed $o (optional) The new data tag to set
     * @return mixed
     */
    public function dataTag($o = false){
        if(func_num_args() == 1){
            $this->dataTag = $o;
        }
        return $this->dataTag;
    }

    /**
     * Get the unique ID of this UI control which can be used as client ID
     * @return string
     */
    public function uniqueId(){
        $baseConverter = new RaiseBaseConverter(16, 36);
        return 'r' . $baseConverter->convert($this->hashCode());
    }

    /**
     * Get or set the RaisePage that this control is bound in
     * @param RaisePage $p (optional) Set the RaisePage that this control is bound in
     * @return RaisePage
     */
    public function page($p = null){
        if(func_num_args() == 1){
            $this->page = $p;
        }
        return $this->page;
    }

    /**
     * @ignore
     * @return string
     */
    function __toString(){
        return $this->output();
    }

}
